package com.application.outdoor.adapter;

import java.io.File;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.application.Activity.R;
import com.application.Controller.NetworkController;
import com.application.Controller.ReadWriteFileController;

public class ContactAdapter extends BaseExpandableListAdapter {

	private Context context;
	private ContactList data;
	String path="";
	public ContactAdapter(Context context, ContactList data) {
		this.context = context;
		this.data = data;
		File dir=context.getDir("avatar", Context.MODE_PRIVATE);
		path=dir.getAbsolutePath();
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		return data.getMember(groupPosition, childPosition);
	}

	@Override
	public long getChildId(int arg0, int arg1) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getChildView(int groupPos, int childPos, boolean isLastChild, View convertView, ViewGroup parent) {
		if (convertView == null) {
			LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = inflater.inflate(R.layout.row_contact_member, null);
		}

		String tenDangNhap = data.getMember(groupPos, childPos).getTenDangNhap();
		TextView txtTenDangNhap = (TextView) convertView.findViewById(R.id.txtName);
		TextView txtDetail = (TextView) convertView.findViewById(R.id.txtDetail);
		txtDetail.setVisibility(View.INVISIBLE);
		ImageView imvAvatar = (ImageView) convertView.findViewById(R.id.imvAvatar);
		txtTenDangNhap.setText(tenDangNhap);
		if (ReadWriteFileController.isFile(path + "/" + tenDangNhap + ".png")) {
			Bitmap bmAvatar = BitmapFactory.decodeFile(path + "/" + tenDangNhap + ".png");
			imvAvatar.setImageBitmap(bmAvatar);
		}else{
			Bitmap bmAvatar = BitmapFactory.decodeResource(context.getResources(), R.drawable.img_user);
			imvAvatar.setImageBitmap(bmAvatar);
		}
		return convertView;
	}

	@Override
	public int getChildrenCount(int groupIndex) {
		return this.data.getData().get(groupIndex).getMemberList().size();
	}

	@Override
	public Object getGroup(int groupIndex) {
		// TODO Auto-generated method stub
		return this.data.getGroup(groupIndex);
	}

	@Override
	public int getGroupCount() {
		return this.data.getSize();
	}

	@Override
	public long getGroupId(int arg0) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {

		if (convertView == null) {
			LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = inflater.inflate(R.layout.row_contac_group, null);
		}

		TextView tvGroupName = (TextView) convertView.findViewById(R.id.txtGroupName);
		tvGroupName.setText(data.getGroup(groupPosition).getTenNhom() + "("
				+ data.getData().get(groupPosition).memberList.size() + ")");

		return convertView;
	}

	@Override
	public boolean hasStableIds() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {

		return true;
	}

}
